package com.xue566.config;

import com.xue566.enums.StateEnum;
import com.xue566.util.ResultBody;
import com.xue566.util.JsonUtil;
import com.xue566.util.StateException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * @Author: Wuzh
 * @Description: 日志切面，通过该切面进行日志捕获和处理
 * @Date Created in 22:56 2020/8/17
 * @return
 */
@Component
@Aspect
@Slf4j
public class LogAspect {

    @Pointcut("execution(public * com.xue566.*.*Controller.*(..))")
    public void pointcut () {
    }

    @Around("pointcut()")
    public Object around (ProceedingJoinPoint point) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String operatorName = getOperatorName(point);
        String reqParam = preHandle(point, request);
        String url = request.getRequestURL().toString();
        log.info("{}-日志号:[{}],请求参数:[{}],请求URL:[{}]", operatorName, reqParam, url);
        long begin = System.currentTimeMillis();
        ResultBody<Object> resultBody = new ResultBody<>();
        Object result = null;
        result = point.proceed();
        String respParam = postHandle(resultBody);
        log.info("{}-请求IP:[{}], 耗时：[{}ms],请求URL:[{}],返回参数:[{}]", operatorName, url, (System.currentTimeMillis() - begin), url, respParam);
        if (reqParam == null) {
            throw new StateException(StateEnum.REQUEST_PARAM_EXCE);
        }
        return result;
    }


    /**
     * @Author: Wuzh
     * @Description: 获取自定义注解value值
     * @Data:
     */
    private String getOperatorName (ProceedingJoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method targetMethod = methodSignature.getMethod();
        LogPointer operateName = targetMethod.getAnnotation(LogPointer.class);
        return operateName.value();
    }


    /**
     * 入参数据
     * @param joinPoint
     * @param request
     * @return
     */
    private String preHandle (ProceedingJoinPoint joinPoint, HttpServletRequest request) {
        String reqParam = "";
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method targetMethod = methodSignature.getMethod();
        Annotation[] annotations = targetMethod.getAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().equals(RequestMapping.class) || annotation.annotationType().equals(GetMapping.class) || annotation.annotationType().equals(PostMapping.class) || annotation.annotationType().equals(PutMapping.class) || annotation.annotationType().equals(DeleteMapping.class)) {
                reqParam = JsonUtil.beanToJsonString(joinPoint.getArgs());
                break;
            }
        }
        return reqParam;
    }


    private String postHandle (Object retVal) throws IOException {
        if (null == retVal) {
            return "";
        }
        return JsonUtil.beanToJsonString(retVal);
    }
}
